<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="SymptomAnalysisForm">
          <a-row>
						<a-col :span="24">
							<a-form-item label="皮肤是否痒" v-bind="validateInfos.skinItch" id="SymptomAnalysisForm-skinItch" name="skinItch">
								<a-input v-model:value="formData.skinItch" placeholder="请输入皮肤是否痒"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="腹部症状" v-bind="validateInfos.abdominalSymptoms" id="SymptomAnalysisForm-abdominalSymptoms" name="abdominalSymptoms">
								<a-input v-model:value="formData.abdominalSymptoms" placeholder="请输入腹部症状"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否容易疲劳" v-bind="validateInfos.fatigue" id="SymptomAnalysisForm-fatigue" name="fatigue">
								<a-input v-model:value="formData.fatigue" placeholder="请输入是否容易疲劳"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="脚部症状" v-bind="validateInfos.footSymptoms" id="SymptomAnalysisForm-footSymptoms" name="footSymptoms">
								<a-input v-model:value="formData.footSymptoms" placeholder="请输入脚部症状"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="视力情况" v-bind="validateInfos.vision" id="SymptomAnalysisForm-vision" name="vision">
								<a-input v-model:value="formData.vision" placeholder="请输入视力情况"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="口渴情况" v-bind="validateInfos.thirst" id="SymptomAnalysisForm-thirst" name="thirst">
								<a-input v-model:value="formData.thirst" placeholder="请输入口渴情况"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="泌尿系统症状" v-bind="validateInfos.urinarySymptoms" id="SymptomAnalysisForm-urinarySymptoms" name="urinarySymptoms">
								<a-input v-model:value="formData.urinarySymptoms" placeholder="请输入泌尿系统症状"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="大便情况" v-bind="validateInfos.stool" id="SymptomAnalysisForm-stool" name="stool">
								<a-input v-model:value="formData.stool" placeholder="请输入大便情况"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="睡眠情况" v-bind="validateInfos.sleepProblem" id="SymptomAnalysisForm-sleepProblem" name="sleepProblem">
								<a-textarea v-model:value="formData.sleepProblem" :rows="4" placeholder="请输入睡眠情况" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="饮食偏好" v-bind="validateInfos.foodPreference" id="SymptomAnalysisForm-foodPreference" name="foodPreference">
								<a-input v-model:value="formData.foodPreference" placeholder="请输入饮食偏好"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="血糖情况" v-bind="validateInfos.diabetes" id="SymptomAnalysisForm-diabetes" name="diabetes">
								<a-textarea v-model:value="formData.diabetes" :rows="4" placeholder="请输入血糖情况" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="其他不适症状" v-bind="validateInfos.otherSymptoms" id="SymptomAnalysisForm-otherSymptoms" name="otherSymptoms">
								<a-textarea v-model:value="formData.otherSymptoms" :rows="4" placeholder="请输入其他不适症状" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="年龄" v-bind="validateInfos.age" id="SymptomAnalysisForm-age" name="age">
								<a-input-number v-model:value="formData.age" placeholder="请输入年龄" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="性别" v-bind="validateInfos.gender" id="SymptomAnalysisForm-gender" name="gender">
								<a-input v-model:value="formData.gender" placeholder="请输入性别"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="体重" v-bind="validateInfos.weight" id="SymptomAnalysisForm-weight" name="weight">
								<a-input-number v-model:value="formData.weight" placeholder="请输入体重" style="width: 100%" />
							</a-form-item>
						</a-col>
            <a-col :span="24">
              <a-form-item label="回复" v-bind="validateInfos.reply" id="SymptomAnalysisForm-reply" name="reply">
                <a-textarea v-model:value="formData.reply" :rows="4" placeholder="请输入" />
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../SymptomAnalysis.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    skinItch: '',   
    abdominalSymptoms: '',   
    fatigue: '',   
    footSymptoms: '',   
    vision: '',   
    thirst: '',   
    urinarySymptoms: '',   
    stool: '',   
    sleepProblem: '',   
    foodPreference: '',   
    diabetes: '',   
    otherSymptoms: '',   
    age: undefined,
    gender: '',   
    weight: undefined,
    reply:'',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
